/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.document.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oyun.annotation.Log;
import com.oyun.config.FileProperties;
import com.oyun.modules.document.domain.BookDocuments;
import com.oyun.modules.document.domain.DocPicture;
import com.oyun.modules.document.domain.vo.BookDocumentsQueryCriteria;
import com.oyun.modules.document.mapper.DocPictureMapper;
import com.oyun.modules.document.service.BookDocumentsService;
import com.oyun.modules.medicine.domain.HpPicture;
import com.oyun.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
* @author oyun
* @date 2024-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "书籍文献管理管理")
@RequestMapping("/api/bookDocuments")
public class BookDocumentsController {

    private final BookDocumentsService bookDocumentsService;


    private final FileProperties properties;
    private final DocPictureMapper docPictureMapper;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bookDocuments:list')")
    public void exportBookDocuments(HttpServletResponse response, BookDocumentsQueryCriteria criteria) throws IOException {
        bookDocumentsService.download(bookDocumentsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询书籍文献管理")
    @ApiOperation("查询书籍文献管理")
    @PreAuthorize("@el.check('bookDocuments:list')")
    public ResponseEntity<PageResult<BookDocuments>> queryBookDocuments(BookDocumentsQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(bookDocumentsService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增书籍文献管理")
    @ApiOperation("新增书籍文献管理")
    @PreAuthorize("@el.check('bookDocuments:add')")
    public ResponseEntity<Object> createBookDocuments(@Validated @RequestBody BookDocuments resources) {
        bookDocumentsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改书籍文献管理")
    @ApiOperation("修改书籍文献管理")
    @PreAuthorize("@el.check('bookDocuments:edit')")
    public ResponseEntity<Object> updateBookDocuments(@Validated @RequestBody BookDocuments resources) {
        bookDocumentsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除书籍文献管理")
    @ApiOperation("删除书籍文献管理")
    @PreAuthorize("@el.check('bookDocuments:del')")
    public ResponseEntity<Object> deleteBookDocuments(@RequestBody List<Long> ids) {
        bookDocumentsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("上传图片")
    @PostMapping("/uploadProcessDefinition/{docId}")
    public ResponseEntity<Object> uploadOrUpdate(MultipartFile file, HttpServletRequest request, @PathVariable("docId") Long id) {
        return new ResponseEntity<>(bookDocumentsService.uploadOrUpdate(file, request, id), HttpStatus.OK);
    }


    @PostMapping("/upload")
    public ResponseEntity<?> uploadFile(@RequestParam("images") MultipartFile[] images, @RequestParam("docId") Long docId) {
        File filee = new File(properties.getPath().getPath() + "/document");
        if (!filee.exists()) {//不存在
            filee.mkdirs();
        }

        for (MultipartFile image : images) {
            File file1 = new File(filee + "/" + image.getOriginalFilename());
            try {
                image.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            DocPicture docPicture = new DocPicture();
            docPicture.setDocId(docId);
            docPicture.setPath("/document/" + image.getOriginalFilename());
            docPicture.setFilename(image.getOriginalFilename());
            docPicture.setStatus(1);
            docPictureMapper.insert(docPicture);
        }
        return ResponseEntity.ok("图片上传成功。");
    }
}
